﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace COFE.Core.Logger
{
    /// <summary>
    /// Listen the COFE Filesystem
    /// </summary>
    public class FileSystemWatcherExA : IFileSystemListener
    {
        #region Constructor
        public FileSystemWatcherExA(string watchPath = null, string[] listeningListers = null)
        {
            if (watchPath != null)
            {
                string pattern = string.Format("{0}.*", watchPath).Replace(@"\",@"\\");
                this.ListenPatterns = new string[] { pattern };
            }
            else
                this.ListenPatterns = new string[] { ".*" };

            this.ListenListers = listeningListers;
        }
        #endregion

        #region Methods

        /// <summary>
        /// Start listening changes.
        /// </summary>
        private void Start()
        {
            COFEShared.FileSystemEventDispatcher.RegisterListener(this);
        }

        /// <summary>
        /// Stop listening changes.
        /// </summary>
        private void Stop()
        {
            COFEShared.FileSystemEventDispatcher.UnregisterListener(this);
        }

        public void DispatchEvent(object sender, FileSystemEventArgsExA args)
        {
            switch (args.ChangeType)
            {
                case ModifiedAction.Created: NotifyCreated(sender, args); break;
                case ModifiedAction.Changed: NotifyChanged(sender, args); break;
                case ModifiedAction.Deleted: NotifyDeleted(sender, args); break;
                case ModifiedAction.Renamed:
                    if (args is RenameEventArgsExA)
                        NotifyRenamed(sender, args as RenameEventArgsExA); break;
            }
        }

        public void NotifyChanged(object sender, FileSystemEventArgsExA args)
        {
            Changed(sender, args);
        }

        public void NotifyCreated(object sender, FileSystemEventArgsExA args)
        {
            Created(sender, args);
        }

        public void NotifyDeleted(object sender, FileSystemEventArgsExA args)
        {
            Deleted(sender, args);
        }

        public void NotifyRenamed(object sender, RenameEventArgsExA args)
        {
            Renamed(sender, args);
        }
        #endregion

        #region Data

        private bool _enableRaisingEvents = false;

        #endregion

        #region Public Properties

        public string[] ListenPatterns { get; private set; }
        public string[] ListenListers { get; private set; }

        public bool EnableRaisingEvents
        {
            get { return _enableRaisingEvents; }
            set { if (_enableRaisingEvents != value) if (value) Start(); else Stop(); _enableRaisingEvents = value; }
        }

        public event EventHandler<FileSystemEventArgsExA> Changed = (o, e) => { };
        public event EventHandler<FileSystemEventArgsExA> Created = (o, e) => { };
        public event EventHandler<FileSystemEventArgsExA> Deleted = (o, e) => { };
        public event EventHandler<RenameEventArgsExA> Renamed = (o, e) => { };

        #endregion



    }
}
